<?php 
class CategoriesController extends AppController{

	var $uses = array( 'Category', 'Task', 'Schedule', 'TasksSchedule', 'CategoriesTask' );


	function view( $intCategoryId ){
		$this->Category->id = $intCategoryId;
		$this->Category->recursive = 2;
		$listCategory = $this->Category->read();
		
		$listProgress = array();
		for( $i=0; $i<=100; ++$i )
			$listProgress[] = $i;
			
		$listDifficulties = array(
			'FACIL' => 'Facil',
			'DIFICIL' => 'Dificil'
		);
		
		$this->set( compact( 'listCategory', 'listDifficulties', 'listProgress' ) );
	}
	
	function setTaskProgress($intTaskId, $intProgress){
		$this->Task->id = $intTaskId;
		$this->Task->saveField( 'progress', $intProgress );
	}
	
	function addTask(){
		if( $this->data ){
			$this->data['Task']['person_id'] = 1; // Temporal
			
			$listStartDate = $this->calculateStartDate();
			
			$this->data['Task']['start_date'] = $listStartDate['start_date'];
			$this->data['Task']['priority'] = 100;
			$this->data['Task']['progress'] = 0;
			
			$this->Task->save( $this->data );
			
			$intTaskId = $this->Task->getInsertID();
			
			$listTasksSchedule = array(
				'task_id' => $intTaskId,
				'date' => $listStartDate['start_date'],
				'start_hour' => $listStartDate['start_hour'],
				'end_hour' => $listStartDate['end_hour'],
				'person_id' => $this->data['Task']['person_id']
			);
			
			$this->TasksSchedule->save( $listTasksSchedule );
			
			$listTaskCategory = array(
				'task_id' => $intTaskId,
				'category_id' => $this->data['Category']['id']
			);
			
			$this->CategoriesTask->save( $listTaskCategory );
			
			$this->Session->setFlash( 'La tarea ha sido creada con exito.' );
		}
		
		$this->redirect( $this->referer() );
	}
	
	
	function deleteTask( $intTaskId ){
		$this->Task->delete( $intTaskId, true );
		$this->Session->setFlash( 'La tarea ha sido eliminada.' );
		$this->redirect( $this->referer() );
	}
	
	private function calculateStartDate(){
		$intPersonId = 1;
		$listDayHours = $this->prepareSchedule();
		
		$listLastTaskSchedule = $this->TasksSchedule->find( 'first', array( 'condtions' => array( 'person_id' => $intPersonId ), 'order' => array( 'date DESC' ) ) );
		if($listLastTaskSchedule){
			$intLastDate = strtotime( $listLastTaskSchedule['TasksSchedule']['date'] );
			$intNextDate = strtotime( '+1 day', $intLastDate );
			$strNextDate = date( 'Y-m-j' , $intNextDate );
			$strDayName = strtolower( date( 'l', $intNextDate ) );
		}else{
			$intNextDate = strtotime( '+1 day', time() );
			$strNextDate = date( 'Y-m-j' , $intNextDate );
			$strDayName = strtolower( date( 'l', $intNextDate ) );
		}
			
		$intStartHour = -1;
		$intEndHour = -1;
		foreach( $listDayHours[ $strDayName ] as $intIndex => $boolIsAvailable ){
			if( $boolIsAvailable ){
				$intStartHour = $intIndex;
				break;
			}
		}
		
		$intEndHour = $intStartHour + $this->data['Task']['estimated_hours'];
		if( $intEndHour > 23 ){
			$intEndHour = 24 - $intEndHour;
		}
		
		
		$listReturn = array(
			'start_date' => $strNextDate,
			'start_hour' => $intStartHour,
			'end_hour' => $intEndHour
		);
		
		
		return $listReturn;
	}
	
	private function prepareSchedule(){
		$intPersonId = 1;
		$listSchedules = $this->Schedule->find( 'all', array( 'recursive' => -1, 'conditions' => array( 'Schedule.person_id' => $intPersonId, 'Schedule.is_bussy' => true ) ) );
		
		$listDays = array( 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' );
		$listDayHours = array();
		
		$listHours = array(true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true);
		foreach( $listDays as $strDay ){
			$listDayHours[ $strDay ] = $listHours;
		}
		
		foreach( $listSchedules as $listSchedule ){
			$listTime = json_decode( $listSchedule['Schedule']['time'] );
			$intFromHour = $listTime->start_time_hour +1;
			$intToHour = $listTime->end_time_hour +1;
			
			if( $intFromHour == 24 )
					$intFromHour = 0;
			if( $intToHour == 24 )
					$intToHour = 0;
			
			do{
				if( $listTime->monday == true )
					$listDayHours['monday'][ $intFromHour ] = 0;
				if( $listTime->tuesday == true )
					$listDayHours['tuesday'][ $intFromHour ] = 0;
				if( $listTime->wednesday == true )
					$listDayHours['wednesday'][ $intFromHour ] = 0;
				if( $listTime->thursday == true )
					$listDayHours['thursday'][ $intFromHour ] = 0;
				if( $listTime->friday == true )
					$listDayHours['friday'][ $intFromHour ] = 0;
				if( $listTime->saturday == true )
					$listDayHours['saturday'][ $intFromHour ] = 0;
				if( $listTime->sunday == true )
					$listDayHours['sunday'][ $intFromHour ] = 0;
						
				++$intFromHour;
				if( $intFromHour == 24 )
					$intFromHour = 0;
			}while( ( $intFromHour != $intToHour ) );
	
		
		}
		
		return $listDayHours;	
	}
}
?>